package com.biboheart.adapter.express.support;

import com.biboheart.adapter.express.actuator.GenericActuator;
import com.biboheart.adapter.support.utils.ObjectTypeUtils;

final class SplitActuator implements GenericActuator {
    @Override
    public Object execute(Object source, Object... args) {
        if (null == source) {
            return null;
        }
        String regex = args.length > 0 ? ObjectTypeUtils.convertType(args[0], String.class) : ",";
        int index = args.length > 1 ? ObjectTypeUtils.convertType(args[1], Integer.class) : 0;
        String sourceStr = String.valueOf(source);
        String[] split = sourceStr.split(regex);
        if (split.length > index) {
            return split[index];
        }
        return null;
    }
}
